package com.lotusfall.mysport;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Pair;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.maps.utils.SpatialRelationUtil;
import com.amap.api.maps.utils.overlay.MovingPointOverlay;
import com.amap.api.maps.utils.overlay.SmoothMoveMarker;
import com.lotusfall.mysport.controller.TrackController;

import java.util.ArrayList;
import java.util.List;

public class replayTrack extends AppCompatActivity {
    TrackController trackController = new TrackController();
    MapView mMapView;
    AMap mAMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<LatLng> points;
        Intent intent = getIntent();
        setContentView(R.layout.activity_replay_track);
        mMapView = (MapView)findViewById(R.id.replayMap);//获取地图控件引用
        mAMap = mMapView.getMap();
        Long id = intent.getLongExtra("trackId",0);
        mMapView.onCreate(savedInstanceState);//创建地图


        // 获取轨迹坐标点
        try {
            points = trackController.getTrackDetail(id);
            System.out.println(points);
            LatLngBounds bounds = new LatLngBounds(points.get(0), points.get(points.size() - 2));
            mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));

            MovingPointOverlay smoothMarker;
            Marker marker = mAMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.user32)).anchor(0.5f,0.5f));
            smoothMarker = new MovingPointOverlay(mAMap, marker);

            Polyline polyline = mAMap.addPolyline(new PolylineOptions().
                    addAll(points).width(10).setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.custtexture)));

            // 设置滑动的图标
            //smoothMarker.setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.user32));

//            LatLng drivePoint = points.get(0);
//            Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint);
//            points.set(pair.first, drivePoint);
//            List<LatLng> subList = points.subList(pair.first, points.size());

            // 取轨迹点的第一个点 作为 平滑移动的启动
            LatLng drivePoint = points.get(0);
            Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint);
            points.set(pair.first, drivePoint);
            List<LatLng> subList = points.subList(pair.first, points.size());

            // 设置轨迹点
            smoothMarker.setPoints(subList);
            // 设置平滑移动的总时间  单位  秒
            smoothMarker.setTotalDuration(3);

            // 设置  自定义的InfoWindow 适配器
            //mAMap.setInfoWindowAdapter(infoWindowAdapter);
            // 显示 infowindow
            marker.showInfoWindow();

            // 设置滑动的轨迹左边点
           // smoothMarker.setPoints(subList);
            // 设置滑动的总时间
            //smoothMarker.setTotalDuration(10);
            // 开始滑动
            smoothMarker.startSmoothMove();





        } catch (InterruptedException e) {
            e.printStackTrace();
        }





    }
}